import os
import shutil
import glob


def files(curr_dir='.', ext='*.exe'):
    for i in glob.glob(os.path.join(curr_dir, ext)):
        yield i

def main(init_path):
    tmppath = init_path + '/tmp'
    if not os.path.exists(tmppath):
        os.mkdir(tmppath)
    os.chdir(tmppath)
    if os.path.exists('rpc-repo'):
        os.chdir('rpc-repo')
        os.system('git pull')
    else:
        os.system('git clone https://gitee.com/dennis-kk/rpc-repo.git')
        os.chdir('rpc-repo')

    for file in files('.', "*.py"):
        shutil.copyfile(file, os.path.join('../../', file))
    if os.path.exists('../../thirdparty'):
        shutil.rmtree('../../thirdparty')
    shutil.copytree("./thirdparty", os.path.join('../../thirdparty'))   

if __name__ == "__main__": 
    exec_file_name = os.path.abspath(__file__)
    print("current working path: ", exec_file_name)
    main(os.path.dirname(exec_file_name))